package com.cwf.auth.controller;

import com.cwf.auth.domain.Role;
import com.cwf.auth.dto.RoleMenuDTO;
import com.cwf.auth.dto.RolePermissionDTO;
import com.cwf.auth.query.RoleQuery;
import com.cwf.auth.service.IRoleService;
import com.cwf.basic.annotation.AutoScanPermission;
import com.cwf.basic.util.AjaxResult;
import com.cwf.basic.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色的API", description = "角色相关的CRUD功能")
@AutoScanPermission(name = "角色管理", desc = "角色管理接口")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     *
     * @param role 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "角色的新增和修改")
    @AutoScanPermission(name = "角色的新增和修改", desc = "角色的新增和修改接口")
    public AjaxResult addOrUpdate(@RequestBody Role role) {
        try {
            if (role.getId() != null)
                roleService.update(role);
            else
                roleService.insert(role);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 授权
     *
     * @param rolePermissionDTO
     * @return
     */
    @PutMapping("/saveAuth")
    @ApiOperation(value = "保存角色权限信息")
    @AutoScanPermission(name = "保存角色权限信息", desc = "保存角色权限信息接口")
    public AjaxResult saveAuth(@RequestBody RolePermissionDTO rolePermissionDTO) {
        try {
            roleService.saveAuth(rolePermissionDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 分配菜单
     *
     * @param roleMenuDTO
     * @return
     */
    @PutMapping("/saveMenu")
    @ApiOperation(value = "保存角色菜单信息")
    @AutoScanPermission(name = "保存角色菜单信息", desc = "保存角色菜单信息接口")
    public AjaxResult saveMenu(@RequestBody RoleMenuDTO roleMenuDTO) {
        try {
            roleService.saveMenu(roleMenuDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据菜单ID删除菜单")
    @AutoScanPermission(name = "根据菜单ID删除菜单", desc = "根据菜单ID删除菜单接口")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            roleService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据角色ID获取角色信息")
    @AutoScanPermission(name = "根据角色ID获取角色信息", desc = "根据角色ID获取角色信息接口")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Role role = roleService.queryById(id);
            return AjaxResult.success(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }

    /**
     * 获取角色已有权限
     *
     * @param id
     * @return
     */
    @GetMapping("/permissions/{id}")
    @ApiOperation(value = "获取角色已有权限")
    @AutoScanPermission(name = "获取角色已有权限", desc = "获取角色已有权限接口")
    public AjaxResult getPermissionsById(@PathVariable("id") Long id) {
        try {
            List<Long> permissions = roleService.getPermissionsById(id);
            return AjaxResult.success(permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }

    @GetMapping("/menus/{id}")
    @ApiOperation(value = "获取角色已有菜单")
    @AutoScanPermission(name = "获取角色已有菜单", desc = "获取角色已有菜单接口")
    public AjaxResult getMenusById(@PathVariable("id") Long id) {
        try {
            List<Long> menus = roleService.getMenusById(id);
            return AjaxResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }

    /**
     * 查看所有的角色信息
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查看所有的角色信息")
    @AutoScanPermission(name = "查看所有的角色信息", desc = "查看所有的角色信息接口")
    public AjaxResult queryAll() {

        try {
            List<Role> list = roleService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    @ApiOperation(value = "分页查询角色信息")
    @AutoScanPermission(name = "分页查询角色信息", desc = "分页查询角色信息接口")
    public AjaxResult queryPage(@RequestBody RoleQuery query) {
        try {
            PageInfo<Role> pageList = roleService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }


    @PatchMapping
    @ApiOperation(value = "批量删除角色信息")
    @AutoScanPermission(name = "批量删除角色信息", desc = "批量删除角色信息接口")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除数据失败！" + e.getMessage());
        }
    }
}
